package com.jiaming.restaurant.api.restaurantgoods.controller;

import com.jiaming.restaurant.api.common.response.ResultCode;
import com.jiaming.restaurant.api.goods.bean.entity.Goods;
import com.jiaming.restaurant.api.restaurantgoods.bean.entity.RestaurantGoods;
import com.jiaming.restaurant.api.restaurantgoods.bean.vo.RestGoodsVO;
import com.jiaming.restaurant.api.restaurantgoods.bean.vo.RestaurantGoodsVO;
import com.jiaming.restaurant.api.restaurantgoods.service.IRestaurantGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author dragon
 * @create 2020-12-28 17:35
 */
@RestController
@RequestMapping(value = "/restaurant_goods")
public class RestaurantGoodsController {

    @Autowired
    IRestaurantGoodsService restaurantGoodsService;

    @PostMapping(value = "/save")
    public Object save(@RequestBody RestaurantGoods restaurantGoods) {
        Map<String, Object> result = new HashMap<>();
        // 参数安全校验

        Integer goodsId = restaurantGoods.getGoodsId();
//        if (goodsId == null || goodsId < 0) {
//            result.put("code", ResultCode.FAIL_ILLEGAL_PARAM);
//            result.put("msg", "参数值不合法");
//            return result;
//        }

        Integer restaurantId = restaurantGoods.getRestaurantId();
//        if (restaurantId == null || restaurantId < 0) {
//            result.put("code", ResultCode.FAIL_ILLEGAL_PARAM);
//            result.put("msg", "参数值不合法");
//            return result;
//        }

        //保存之前，先检查数据库是否已经有一样的数据

        RestaurantGoods rd = restaurantGoodsService.getByRestaurantIdAndGoodsId(restaurantId, goodsId);
        if (rd != null) {
            result.put("code", ResultCode.FAIL);
            result.put("msg", "商品已存在");
            return result;
        }
        boolean isOk = restaurantGoodsService.save(restaurantGoods);
        if (!isOk) {
            result.put("code", ResultCode.FAIL);
        }
        result.put("code", ResultCode.SUCCESS);
        result.put("data", restaurantGoods.getId());
        return result;
    }

    @PostMapping(value = "/updateById")
    public Object update(@RequestBody RestaurantGoods restaurantGoods) {
        Map<String, Object> result = new HashMap<>();
        // 参数安全校验
        Integer goodsId = restaurantGoods.getGoodsId();
//        if (goodsId == null || goodsId < 0) {
//            result.put("code", ResultCode.FAIL_ILLEGAL_PARAM);
//            result.put("msg", "参数值不合法");
//            return result;
//        }
        Integer restaurantId = restaurantGoods.getRestaurantId();
//        if (restaurantId == null || restaurantId < 0) {
//            result.put("code", ResultCode.FAIL_ILLEGAL_PARAM);
//            result.put("msg", "参数值不合法");
//            return result;
//        }
        Integer id = restaurantGoods.getId();
//        if (id == null || id <= 0) {
//            result.put("code", ResultCode.FAIL_ILLEGAL_PARAM);
//            result.put("msg", "参数值不合法");
//            return result;
//        }
        //TODO 更新之前，检查表中是否已经除自己记录外的一样数据
        RestaurantGoods rdme = restaurantGoodsService.getByRestaurantIdAndGoodsIdAndId(id, restaurantId, goodsId);
        if (rdme != null) {
            result.put("code", ResultCode.FAIL);
            result.put("msg", "商品无需修改");
            return result;
        }
        RestaurantGoods rd = restaurantGoodsService.getByRestaurantIdAndGoodsId(restaurantId, goodsId);
        if (rd != null) {
            result.put("code", ResultCode.FAIL);
            result.put("msg", "商品已存在");
            return result;
        }
        boolean isOk = restaurantGoodsService.updateById(restaurantGoods);
        if (!isOk) {
            result.put("code", ResultCode.FAIL);
        }
        result.put("code", ResultCode.SUCCESS);
        return result;
    }

    @PostMapping(value = "/deleteById")
    public Object get(@RequestParam Integer id) {
        Map<String, Object> result = new HashMap<>();
//        if (id <= 0) {
//            result.put("code", ResultCode.FAIL_ILLEGAL_PARAM);
//            result.put("msg", "参数值不合法");
//            return result;
//        }
        boolean isOk = restaurantGoodsService.removeById(id);
        if (!isOk) {
            result.put("code", ResultCode.FAIL);
        }
        result.put("code", ResultCode.SUCCESS);
        return result;
    }

    @PostMapping(value = "/list")
    public Object list(@RequestBody RestaurantGoodsVO restaurantGoodsVO) {
        Map<String, Object> result = new HashMap<>();
//        Integer restaurantId = restaurantGoodsVO.getRestaurantId();
//        if (restaurantId == null || restaurantId < 0) {
//            result.put("code", ResultCode.FAIL_ILLEGAL_PARAM);
//            result.put("msg", "参数值不合法");
//            return result;
//        }

        List<RestGoodsVO> restGoodsVOS = restaurantGoodsService.getByRestId(restaurantGoodsVO);
        result.put("code", ResultCode.SUCCESS);
        result.put("data", restGoodsVOS);
        return result;
    }

    @PostMapping(value = "/getById")
    public Object getByRest(@RequestParam Integer id) {
        Map<String, Object> result = new HashMap<>();
        RestaurantGoods byId = restaurantGoodsService.getById(id);
        result.put("code", ResultCode.SUCCESS);
        result.put("data", byId);
        return result;
    }
}
